$(function(){
	//若用户名或角色为空则需要重新登录cas
	if($("#userCount_id").val()=='null'||$('#roleName_id').val()=='null'){
		drgUI.drgLogOut();
		return;
	}
  /*	验证License*/
	$.post(contextPath + "/auth/currentUser/checkLicense.dhc").done(function(res) {
		var lmsg=res.lmsg;
		if(lmsg=='success') {
			/*获取分组器对应的标杆类型*/
			$.post(contextPath + "/systemConfig/getGroupMatchStd.dhc")
				.done(function(result) {
				if(result.success && result.data){
					groupMatchStd=result.data;
				}
			});
			/* 获取分组方案和测算标杆 */
			$.post(contextPath + "/systemConfig/getSystemGroupScheme.dhc").done(function(result) {
				if (result.success) {
					var roleName=$('#roleName_id').val();
					var groupScheme = result.data;
					if (groupScheme != null) {
						/* 设置分组方案 */
						setGroupScheme(groupScheme);
					} 
					if($.inArray(roleName, hideSysConfig)!=-1){
						$('#setSysConfig').css('display','none');
						loadHomePageMenu();
					}else{
						if (groupScheme != null) {
							/* 加载首页和菜单信息 */
							loadHomePageMenu();
						} else {
							drgUI.alert('请点击【确定】配置分组方案！', function() {
								switchDialog.show();
							});
						}
					}
				}
			});
		}else{
				loadContent($('#home'), '/pages/welcome.jsp');
				drgUI.warning(lmsg);
		}
	});
});
/* 加载首页和菜单信息 */
function loadHomePageMenu() {
	// 主页信息, 获取角色对应的首页设置
	var roleName = $("#roleName_id").val();
	if (null != roleName) {
		/*加载首页*/
		$.post(contextPath + "/auth/currentUser/getHomePageByRole.dhc", {
			role : roleName.trim()
		}).done(function(data) {
			if (data.success && data.data) {
				tabId = "home";
				loadTabParam(tabId);
				$('#navTabs').find('li:first').attr('data-target',data.data);
				loadContent($('#home'), data.data);
				var content = $('#home');
				paramTabPage[tabId].prevTitle = "首页";
				content.data(paramTabPage[tabId]);
			} else {
				$('#navTabs').find('li:first').attr('data-target','/pages/welcome.jsp');
				loadContent($('#home'), '/pages/welcome.jsp');
			}
		});
	}
	commonLayerConfig = getLayerConfig();
	doctorSetConfig = getDoctorConfig();
	//获取需要修改的菜单
	if (roleName == "医政") {
		adjustUrl["yizheng"] = {};
		if (commonLayerConfig[0] == "1") {
			//科室指标检测页面
			adjustUrl.yizheng["depIndexDetec"] = "/pages/yizheng/ywcDepGroup.jsp";
			//绩效分析
			adjustUrl.yizheng["perfAnalysis"] = "/pages/yizheng/perf/ywcHospPerf.jsp";
			//医保测算
			adjustUrl.yizheng["foreCast"] = "/pages/yizheng/quotaComputer/yizhengForecast.jsp";
		} else if (commonLayerConfig[1] == "1") {
			//科室指标检测页面
			adjustUrl.yizheng["depIndexDetec"] = "/pages/yizheng/ywcDep.jsp";
			//病例分析页面
			adjustUrl.yizheng["perfAnalysis"] = "/pages/yizheng/perf/ywcDepPerf.jsp";
			//医保测算
			adjustUrl.yizheng["foreCast"] = "/pages/yizheng/quotaComputer/yizhengForecast_layerdep.jsp";
		} else if (commonLayerConfig[2] == "1") {
			//科室指标检测页面
			adjustUrl.yizheng["depIndexDetec"] = "/pages/yizheng/ywcWard.jsp";
			//绩效分析
			adjustUrl.yizheng["perfAnalysis"] = "/pages/yizheng/perf/ywcWardPerf.jsp";
		}
	}
	if (roleName == "医保") {
		adjustUrl["yibao"] = {};
		if (commonLayerConfig[0] == "1") {
			//科室指标检测页面
			adjustUrl.yibao["depIndexDetec"] = "/pages/yibao/keshi/ybbDepGroup.jsp";
			//医保测算
			adjustUrl.yibao["foreCast"] = "/pages/yibao/quotaComputer/yibaoForecast.jsp";
		} else if (commonLayerConfig[1] == "1") {
			//科室指标检测页面
			adjustUrl.yibao["depIndexDetec"] = "/pages/yibao/keshi/ybbDep.jsp";
			//医保测算
			adjustUrl.yibao["foreCast"] = "/pages/yibao/quotaComputer/yibaoForecast_layerdep.jsp";
		} else if (commonLayerConfig[2] == "1") {
			//科室指标检测页面
			adjustUrl.yibao["depIndexDetec"] = "/pages/yibao/keshi/ybbWard.jsp";
		}
	}
	//根据用户角色加载菜单
	var url = contextPath + "/auth/currentUser/findAllMenusByUserAsRole.dhc?" + new Date().getTime();
	$.get(url,function(data) {
	var currentNum = 0;//当前是一级菜单的顺序
	$.each(data.data,function() {
						//加载一级菜单
						var $li = null;
//						if (0 == currentNum) {//第一个展开
//							$li = $('<li class="folder"><a data-toggle="collapse" href="#menuMark'+this.id+'"><span class="'+this.menuIcon+'"></span>&nbsp;'
//									+ this.name
//									+ '&nbsp;'
//									+ '<i class="glyphicon icon-caret-down" style=" float: right;font-size: 12px;position: relative;right: 8px;top: 3px;"></i></a><ul id="menuMark'+this.id+'" class="second-level-menu collapse in"></ul></li>');
//							$('.first-level-menu').append($li);
//						} else {//其他的一级菜单默认关闭
							$li = $('<li class="folder"><a data-toggle="collapse" classe = "collapsed" href="#menuMark'+this.id+'"><span class="'+this.menuIcon+'"></span>&nbsp;'
									+ this.name
									+ '&nbsp;'
									+ '<i class="glyphicon icon-caret-left" style=" float: right;font-size: 12px;position: relative;right: 8px;top: 3px;"></i></a><ul id="menuMark'+this.id+'" class="second-level-menu collapse"></ul></li>');
							$('.first-level-menu').append($li);
//						}
						currentNum = currentNum + 1;
						//加载子菜单
						renderSubMenu(this.children, $li);
					});

	/*菜单收缩样式变化 */
	var firstLevelMenu = $('.first-level-menu');
	firstLevelMenu.find('[data-toggle="collapse"]').each(function() {
		var $this = $(this);
		firstLevelMenu.find($(this).attr('href')).on({
			'shown.bs.collapse' : function(e) {
				$this.find('i:last').addClass('icon-caret-down').removeClass('icon-caret-left');
			},
			'hidden.bs.collapse' : function(e) {
				$this.find('i:last').removeClass('icon-caret-down').addClass('icon-caret-left');
			}
		});
	});

	/*菜单点击展开，关闭其它菜单*/
	firstLevelMenu.find('[data-toggle="collapse"]').click(function() {
		//获取当前已经打开的菜单ID
		var hrefSelected = firstLevelMenu.find('ul[class="second-level-menu collapse in"]').attr("id");
		//获取当前点击菜单ID 
		var hrefSelecting = $(this).attr('href').substring(1);

		//菜单不同的情况下关闭原来的菜单
		if (hrefSelected != hrefSelecting) {
			$('a[href="#' + hrefSelected + '"]').addClass("collapsed");
			$('a[href="#' + hrefSelected + '"]').find('i:last').removeClass('icon-caret-down').addClass('icon-caret-left')
			$("#" + hrefSelected).removeClass("in");
		}

		setTimeout(function() {
			var cdheight = 0;
			$('.g-sidec>ul>li').each(function() {
				cdheight += $(this).height();
			})
			$('.g-sidec').height(cdheight);
		}, 400)

	});

}, "json");
}



/**
 * 渲染菜单
 */
var renderSubMenu = function(data, $menu) {
	var roleName = $("#roleName_id").val();
	$.each(data,function() {
		if (this.children.length > 0) {
			var $li = $('<li class="folder"><a data-toggle="collapse" href="#menuMark'+this.id+'"><span class="'+this.menuIcon+'"></span>&nbsp;'
					+ this.name
					+ '&nbsp;'
					+ '<i class="glyphicon icon-caret-left pull-right" style="position: relative; right: 12px;font-size: 12px;"></i></a><ul id="menuMark'+this.id+'" class="second-level-menu collapse"></ul></li>');
			$li.appendTo($menu.find('.second-level-menu:first')).find('a').css('padding-left', parseInt(this.level + 1) * 18 + 'px');
			renderSubMenu(this.children, $li);
		} else {
			if (roleName == "医政") {
				if (this.name == '科室指标监测') {
					this.url = adjustUrl.yizheng.depIndexDetec;
				}else if(this.name == "管理目标值测算"){
					this.url = adjustUrl.yizheng.foreCast;
				}else if (this.name == '绩效分析') {
					this.url = adjustUrl.yizheng.perfAnalysis;
				}
			} else if (roleName == "医保") {
				if (this.name == '科室指标监测') {
					this.url = adjustUrl.yibao.depIndexDetec;
				}else if(this.name == "医保测算"){
					this.url = adjustUrl.yibao.foreCast;
				}
			}
			var $li = $(' <li class="submenu" data-role="openTab" data-target="'+this.url+'" data-title="'+this.name+'" ' +
          'data-mark="menuMark'+this.id+'"><a ><span class="'+this.menuIcon+'"></span>&nbsp;'
					+ this.name + '</a></li>');
			$li.appendTo($menu.find('.second-level-menu:first')).find('a').css('padding-left', parseInt(this.level + 1) * 18 + 'px');

		}
	});
	$menu.find('[data-toggle="collapse"]').each(function() {
		var $this = $(this);
		$menu.find($(this).attr('href')).on({
			'shown.bs.collapse' : function(e) {
				e.stopPropagation();
				e.preventDefault();
				$this.find('i:last').addClass('icon-caret-down').removeClass('icon-caret-left');
			},
			'hidden.bs.collapse' : function(e) {
				e.stopPropagation();
				e.preventDefault();
				$this.find('i:last').removeClass('icon-caret-down').addClass('icon-caret-left');
			}
		});
	});
	$menu.find('li.submenu').on('click', function() {
		var $this = $(this);
		$('.first-level-menu').find('li').each(function() {
			var $menuLi = $(this);
			$menuLi.hasClass('active') && $menuLi.removeClass('active').parent().parent().removeClass('active');
		});
		$this.addClass('active').parents().filter('.folder').addClass('active');
		var target = $this.data('target');
		var title = $this.data('title');
		var mark = $this.data('mark');
		if (target && title && mark) {
			openTab(target, title, mark);
			/*点击菜单时判断配置是否显示*/
			showSysConfigByUrl(target);
		}
	});
};

/*
加载DIV内容
*/
var loadContent = function(obj, url) {
	/*根据页面隐藏页面右上角的配置*/
	showSysConfigByUrl(url);
	// 初始化参数获取
	loadTabParam(tabId);
	$.get(contextPath + url).done(function(data, status, objXMLHttp) {
		var headers = objXMLHttp.getAllResponseHeaders();
		if (headers.indexOf("login: login") >= 0
				&& window.location.pathname.indexOf("/login.dhc") < 0) {
			window.location.href = contextPath + "/login.dhc";
		} else {
			obj.html(data);
			$('#tabContent').trigger('loadContentCompalte', obj);
		}
		// 页面钻取时定位
		var num_i = 0;
		$('#' + tabId).ajaxStop(function() {
			if (num_i == 0) {
				loadNav();
				// 显示上级页面tab页打开情况
				if (new Base64().decode($(this).attr('id')).indexOf('menu') < 0) {
					autolocation.showTabs(tabId);
				}
			}
			num_i++;
		})
		// 切换页签时定位
		$('#navTabs a[data-toggle="tab"]').off('shown.bs.tab');
		$('#navTabs a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
			var $ul = $(this).closest('ul:not(.dropdown-menu)');
			var url=$ul.find('.active:first').attr('data-target');
			showSysConfigByUrl(url);
			var previous = $ul.find('.active:last a')[0]
			relatedTarget : previous
			if (localStorage.navTabs != undefined) {
				var nav = JSON.parse(localStorage.navTabs);
				tabId = $("#tabContent > .tab-pane.active").attr("id");
				var index = nav['tab' + tabId];
				$('#' + tabId + ' .nav-item[data-index="' + index + '"]').click();
			}
		});
	}).fail(function(jqXHR, textStatus, errorThrown) {
			if(jqXHR.status==0){
				drgUI.drgLogOut();
				return;
			}
			throw new Error('加载失败');
			}).always(function() {
				changeHeight();
			});
 //监测对于同一个时间段内有多个版本的分组结果
  $.post(contextPath + "/DrgsIndexCal/checkVerVersion.dhc",
	paramTabPage[tabId]).done(function(result) {
		if (result.success && result.data && result.data.length>1) {
			drgUI.alert(paramTabPage[tabId].dateFrom+'~'+paramTabPage[tabId].dateTo+'时间段内存在多个版本的分组结果，原则上只能选用一版分组器，现已选用病例数最多的【'+result.data[0].VER_CODE+'】分组器对应的标杆进行计算，但存在分析结果不准确，请尽快修正数据分组情况！');
		} 
	});
};
/**
 * @ Discription:根据url选择是否显示配置
 * @Autor ：liuhongjun
 * @Date  ：2018年3月18日下午5:52:31
 */
function showSysConfigByUrl(url){
	var roleName=$('#roleName_id').val();
	//如果当前角色不在隐藏的配置中再判断url是否显示【配置】
	if($.inArray(roleName, hideSysConfig)==-1){
		if(url){
			var urlstr=url.split('/');
			if(urlstr.length>0 && $.inArray(urlstr[urlstr.length-1], hideSysConfig)!=-1){
				$('#setSysConfig').css('display','none');
			}else{
				$('#setSysConfig').css('display','block');
			}
		}else{
			$('#setSysConfig').css('display','block');
		}
	}else{
		if(url){
			var urlstr=url.split('/');
			//如果页面在showSysConfig里则显示
			if(urlstr.length>0 && $.inArray(urlstr[urlstr.length-1], showSysConfig)!=-1){
				$('#setSysConfig').css('display','block');
			}else{
				//当前角色不在隐藏的配置中显示【配置】
				$('#setSysConfig').css('display','none');
			}
		}else{
			$('#setSysConfig').css('display','block');
		}
	}
}